package com.tyun.modules.cms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tyun.common.aspect.BaseLog;
import com.tyun.common.utils.R;
import com.tyun.modules.cms.entity.CmsCategory;
import com.tyun.modules.cms.service.ICmsCategoryService;
import com.tyun.modules.sys.controller.CommonController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 *
 * @author congcong tao
 * @date 2018/6/21 17:09
 */
@RestController
@RequestMapping("cmsCategory")
public class CmsCategoryController extends CommonController{

    private static final Logger logger = LogManager.getLogger(CmsCategoryController.class);

    @Autowired
    private ICmsCategoryService cmsCategoryService;

    @GetMapping("/page")
    public Page<CmsCategory> page(Page<CmsCategory> page, CmsCategory cmsCategory){
        cmsCategoryService.selectPage(page, new EntityWrapper<>(cmsCategory));
        return page;
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        CmsCategory category = cmsCategoryService.selectById(id);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @BaseLog("保存菜单")
    @PostMapping("/save")
    public R save(@RequestBody CmsCategory category){
        boolean flag = cmsCategoryService.insert(category);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @BaseLog("修改菜单")
    @PutMapping("/update")
    public R update(@RequestBody CmsCategory category){
        boolean flag = cmsCategoryService.updateById(category);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{menuId}")
    public R delete(@PathVariable String id){
        boolean flag = cmsCategoryService.deleteById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
